<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['stars']=array(
		'1'=>'一星',
		'2'=>'二星',
		'3'=>'三星',
		'4'=>'四星',
		'5'=>'五星',
);
$tabs=array(
	array('op'=>'list','title'=>'评价列表'),
	array('op'=>'edit','title'=>'添加评价'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['merchid'] = $merchid;
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_yiliao_pingjia',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=model('core/member')->detail($item['mid'],array('credit1','credit2','mobile','groupid','avatar','nickname'));
			$item['member']=$member;
			if($item['type']==0){
				$item['username']=pdo_getcolumn('jy_yiliao_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']),'name');
			}else{
				$item['username']=pdo_getcolumn('jy_yiliao_worker',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']),'name');
			}
			$item['workertitle'] = pdo_getcolumn('jy_yiliao_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'name');
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
		
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'member','title'=>'粉丝信息','type'=>'member'),
			array('name'=>'content','title'=>'评价内容','type'=>'string'),
			array('name'=>'star','title'=>'评价星级','type'=>'radio','options'=>'stars'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'workertitle','title'=>'评价医生','type'=>'string'),
			array('name'=>'status','title'=>'是否显示','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'username','help'=>'患者姓名','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		//var_dump($merchid);
		//die;
		$update=array(
			'content'=>$post['content'],
			'images'=>$post['images']?ijson_encode($post['images']):'',
			'status'=>$post['status'],
			'star'=>$post['star'],
			'name'=>$post['name'],
			'merchid'=>$merchid
		);
		
		$id=intval($post['id']);
		if($id){
		    $update['user_id']=$post['worker_id'];
			$update['worker_id']=$post['worker_id'];
			pdo_update('jy_yiliao_pingjia',$update,array('id'=>$id));
		}else{
			
			if($post['createtime']){
				$mins=rand(1,59);
				$createtime=strtotime($post['createtime'])+$mins;
			}
			$update['uniacid']=$_SESSION['uniacid'];
			$update['mid']=$post['mid']?:$mid;
			$update['user_id']=$post['worker_id'];
			$update['worker_id']=$post['worker_id'];
	       
			$update['createtime']=$createtime?:TIMESTAMP;
			pdo_insert('jy_yiliao_pingjia',$update);
		}
		exi('操作成功！','','goback');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		$item=array(
			'star'=>'5',
		);
		$item['images']=$item['images']?json_decode($item['images'],true):array();
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'tabs'=>$tabs,
			'edit'=>array(
			
				array('name'=>'name','title'=>'会员姓名','type'=>'string'),
				array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'jy_yiliao/merch.get_member/','choose_title'=>'选择会员','help'=>'这里只能选择虚拟会员！不会显示真实会员！'),
				array('type'=>'choose','title'=>'选择医生','name'=>'worker_id','action'=>'jy_yiliao/merch.get_worker/','choose_title'=>'选择医生'),				
				array('name'=>'createtime','title'=>'指定评价时间','type'=>'time'),
				array('name'=>'star','title'=>'评价星级','type'=>'radio','options'=>'stars'),
				array('name'=>'content','title'=>'评价内容','type'=>'text'),
				array('name'=>'images','title'=>'晒图','type'=>'images'),
				array('name'=>'status','title'=>'是否显示','type'=>'switch'),
			),
		);	
	}else{
		$item=pdo_get('jy_yiliao_pingjia',array('id'=>$id));
		$item['goods']=$this->pdo_get('goods',array('id'=>$item['goodsid']),array('title','image'));
		$item['images']=$item['images']?json_decode($item['images'],true):array();
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'tabs'=>$tabs,
			'edit'=>array(
				array('name'=>'name','title'=>'会员姓名','type'=>'string'),
				array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'jy_yiliao/merch.get_member/','choose_title'=>'选择会员','help'=>'这里只能选择虚拟会员！不会显示真实会员！'),
				array('type'=>'choose','title'=>'选择医生','name'=>'worker_id','action'=>'jy_yiliao/merch.get_worker/','choose_title'=>'选择医生'),		
				array('name'=>'createtime','title'=>'指定评价时间','type'=>'time'),
				array('name'=>'star','title'=>'评价星级','type'=>'radio','options'=>'stars'),
				array('name'=>'content','title'=>'评价内容','type'=>'text'),
				array('name'=>'images','title'=>'晒图','type'=>'images'),
				array('name'=>'status','title'=>'是否显示','type'=>'switch'),
			),
		);	
	}	
	exi($return);
}

if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('jy_yiliao_pingjia',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_yiliao_pingjia',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_yiliao_pingjia',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}